<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改商品')" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-goods-edit" th:object="${storeGoods}">
            <input name="goodsId" th:field="*{goodsId}" type="hidden">
            <div class="form-group">
                <label class="col-sm-2 control-label"><span style="color: red; ">*</span>分类：</label>
                <div class="col-sm-4">
                    <div class="input-group">
                        <input id="treeId" name="cateId" type="hidden" th:value="*{cateId}"/>
                        <input  onclick="selectCateTree()" id="treeName" type="text" placeholder="请选择分类" class="form-control" th:value="*{cateName}" required>
                        <span class="input-group-addon"><i class="fa fa-search"></i></span>
                    </div>
                </div>
                <label class="col-sm-2 control-label">商家：</label>
                <div class="col-sm-4">
                    <select class="form-control" name="shopId" id="selectObj2">

                    </select>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-2 control-label">商品编号：</label>
                <div class="col-sm-4">
                    <input name="goodsSn" th:field="*{goodsSn}" class="form-control" type="text" required>
                </div>
                  <label class="col-sm-2 control-label">商品名称：</label>
                <div class="col-sm-4">
                    <input name="goodsName" th:field="*{goodsName}" class="form-control" type="text" required>
                </div>
            </div>
          
            <div class="form-group">    
                <label class="col-sm-2 control-label">售价：</label>
                <div class="col-sm-4">
                    <input name="marketPrice" th:field="*{marketPrice}" class="form-control" type="number" required>
                </div>
                <label class="col-sm-2 control-label">原价：</label>
                <div class="col-sm-4">
                    <input name="shopPrice" th:field="*{shopPrice}" class="form-control" type="number" required>
                </div>
            </div>
             <div class="form-group">    
                <label class="col-sm-2 control-label">折扣（0-1）：</label>
                <div class="col-sm-4">
                    <input name="discounts" class="form-control" type="number"  th:field="*{discounts}" required>
                </div>
                <label class="col-sm-2 control-label">颜色种类：</label>
                <div class="col-sm-4">
                    <input name="colorNum" class="form-control" type="text" th:field="*{colorNum}" required>
                </div>
            </div>
             <div class="form-group">
             <label class="col-sm-2 control-label">运费：</label>
                <div class="col-sm-4">
                    <input name="freePrice" th:field="*{freePrice}"  class="form-control" type="number" required>
                </div>    
                <label class="col-sm-2 control-label">赠送积分：</label>
                <div class="col-sm-4">
                    <input name="giveIntegral" th:field="*{giveIntegral}"  class="form-control"  type="number" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-2 control-label">商品关键词：</label>
                <div class="col-sm-10">
                    <input name="keywords" th:field="*{keywords}" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">商品描述：</label>
                <div class="col-sm-10">
                    <textarea name="goodsRemark" class="form-control" required>[[*{goodsRemark}]]</textarea>
                </div>
            </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">商品(主图)</label>
                <div class="col-sm-10">
                    <input name="goodsLogo" id="imgUrl" class="form-control" th:field="*{goodsLogo}"  type="hidden">
                    <input id="fileinput-demo-1" name="file" type="file" multiple>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">商品详情：</label>
                <div class="col-sm-10">
                    <input id="goodsContent" name="goodsContent" th:field="*{goodsContent}" type="hidden">
                    <div class="summernote" id="editor"></div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-2 control-label">商品排序：</label>
                <div class="col-sm-10">
                    <input name="sort" th:field="*{sort}" class="form-control" type="text" required>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-css" />
    <th:block th:include="include :: summernote-css" />
    <th:block th:include="include :: bootstrap-fileinput-js" />
    <th:block th:include="include :: summernote-js" />
    <script type="text/javascript">
        var prefix = ctx + "shop/goods";
        $("#form-goods-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
            	var sHTML = $('.summernote').summernote('code');
				$("#goodsContent").val(sHTML);
                $.operate.save(prefix + "/edit", $('#form-goods-edit').serialize());
            }
        }

        function selectCateTree() {
        	var cateId = $.common.isEmpty($("#treeId").val()) ? "100" : $("#treeId").val();
            var url = ctx + "shop/cate/selectCateTree/" + cateId;
            var options = {
                title: '商城商品分类选择',
                width: "380",
                url: url,
                callBack: doSubmit
            };
            $.modal.openOptions(options);
        }
        function doSubmit(index, layero){
			var tree = layero.find("iframe")[0].contentWindow.$._tree;
			var body = $.modal.getChildFrame(index);
   			$("#treeId").val(body.find('#treeId').val());
   			$("#treeName").val(body.find('#treeName').val());
   			$.modal.close(index);
		} 

        $('.summernote').summernote({
            placeholder: '请输入内容',
            height : 192,
            lang : 'zh-CN',
            followingToolbar: false,
            callbacks: {
                onImageUpload: function (files) {
                    sendFile(files[0], this);
                }
            }
        });
        var content = $("#goodsContent").val();
	    $('#editor').summernote('code', content);

        // 上传文件
        function sendFile(file, obj) {
            var data = new FormData();
            data.append("file", file);
            $.ajax({
                type: "POST",
                url: ctx + "common/upload",
                data: data,
                cache: false,
                contentType: false,
                processData: false,
                dataType: 'json',
                success: function(result) {
                    if (result.code == web_status.SUCCESS) {
                        $(obj).summernote('editor.insertImage', result.url, result.fileName);
                    } else {
                        $.modal.alertError(result.msg);
                    }
                },
                error: function(error) {
                    $.modal.alertWarning("图片上传失败。");
                }
            });
        }

        $(document).ready(function () {
            $("#fileinput-demo-1").fileinput({
                'theme': 'explorer-fas',
                'uploadUrl': '/common/upload',
                overwriteInitial: false,
                initialPreviewAsData: true,
                initialPreview: [
                    "[[${storeGoods.goodsLogo}]]"
                ]
            });

            $("#fileinput-demo-1").on("fileuploaded", function (event, data, previewId, index) {
                console.log(data.response);
                if(data.response.code == 0){
                    $('#imgUrl').val(data.response.url);
                }
            });



            //商家
            var options2 = [];
            var selectObj2 = $("#selectObj2");
            var optionValue2 = 0;
            var optionName2 = '';
            var row2 = null;
            var optionStr2 = '';
            var thisshopId = [[${storeGoods.shopId}]];
            var config2 = {
                url: "/shop/news/list1",
                type: "post",
                dataType: "json",
                data: {pageSize:100},
                success: function(data) {
                    for (var i = 0;i < data.total;i++) {
                        row2 = data.rows[i];
                        optionValue2 = row2.id;
                        optionName2 = row2.name;
                        if(optionValue2 == thisshopId){
                            options2.push("<option value="+optionValue2+" selected>"+optionName2+"</option>");
                        }else{
                            options2.push("<option value="+optionValue2+">"+optionName2+"</option>");
                        }
                    }
                    optionStr2 = options2.join('');

                    selectObj2.html(optionStr2);
                }
            };
            $.ajax(config2)

        });

    </script>
</body>
</html>